package dcc.component.tabs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.TableCellEditor;

import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;

import dcc.component.DeleteButtonRenderer;

public class TablePanel<E> extends JPanel {

	private String id;
	private JScrollPane scrollPane;
	private JTable table;
	private String[] headers;
	private String[] fields;
	private Command parent;
	private List<E> list;

	public interface Command {
		Object addRow();

		void resetWidth(JTable table);

		void deleteRow(int id);
	}

	public TablePanel(Command parent) {
		this.parent = parent;
		this.setLayout(new BorderLayout(0, 0));
		scrollPane = new JScrollPane();
		scrollPane.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (SwingUtilities.isRightMouseButton(e)) {
					addNewRecord(addRow());
				}
			}
		});
		this.add(scrollPane, BorderLayout.CENTER);
		this.table = createTable();
		scrollPane.setViewportView(table);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setPreferredSize(new Dimension(10, 30));
		add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		JLabel label = new JLabel("*\u63D0\u793A\uFF1A\u70B9\u51FB\u9F20\u6807\u53F3\u952E\u53EF\u4EE5\u6DFB\u52A0\u4E00\u884C\u65B0\u7EAA\u5F55");
		label.setBackground(Color.WHITE);
		label.setBorder(new EmptyBorder(0, 20, 0, 0));
		label.setForeground(Color.BLUE);
		panel.add(label, BorderLayout.CENTER);
	}

	private E addRow() {
		return (E) parent.addRow();
	}

	public JTable getTable() {
		return this.table;
	}

	private JTable createTable() {
		JTable table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (SwingUtilities.isRightMouseButton(e)) {
					addNewRecord(addRow());
				}
			}
		});
		table.setBackground(Color.WHITE);
		table.setRowHeight(30);
		table.setBorder(new LineBorder(new Color(0, 0, 0)));
		table.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13));
		table.getTableHeader().setPreferredSize(new Dimension(0, 30));
		table.getTableHeader().setFont(
				new Font("Microsoft YaHei", Font.PLAIN, 13));
		table.getTableHeader().setBackground(Color.WHITE);
		table.getTableHeader().setBorder(new LineBorder(new Color(0, 0, 0)));

		return table;
	}

	public void setHeader(String[] headers, String[] fields, String id) {
		this.headers = headers;
		this.fields = fields;
		this.id = id;
	}

	public void setData(List<E> list) {
		this.list = list;
		initDataBindings(this.list);
	}

	private JTableBinding<E, List<E>, JTable> jTableBinding;

	private void initDataBindings(List<E> list) {
		jTableBinding = SwingBindings.createJTableBinding(UpdateStrategy.READ,
				list, this.table, "bindingRole");
		for (int i = 0; i < this.fields.length; i++) {
			BeanProperty<E, String> f = BeanProperty.create(fields[i]);
			jTableBinding.addColumnBinding(f).setColumnName(this.headers[i])
					.setEditable(true);
		}
		//
		BeanProperty<E, String> deleteButtonBeanProperty = BeanProperty
				.create(this.id);
		jTableBinding.addColumnBinding(deleteButtonBeanProperty).setColumnName(
				"\u64CD\u4F5C");
		jTableBinding.bind();

		this.table.getColumnModel().getColumn(fields.length).setMaxWidth(40);
		this.table.getColumnModel().getColumn(fields.length)
				.setCellRenderer(new DeleteButtonRenderer());
		this.table.getColumnModel().getColumn(fields.length)
				.setCellEditor(new DeleteButtonEditor(this.table, parent));
		this.parent.resetWidth(this.table);
	}

	private class DeleteButtonEditor extends AbstractCellEditor implements
			TableCellEditor, ActionListener {

		public DeleteButtonEditor(JTable table, Command parent) {
			button = new JButton("\u5220\u9664");
			button.addActionListener(this);
		}

		private JButton button;

		@Override
		public Object getCellEditorValue() {
			return button.getActionCommand();
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			String[] commands = button.getActionCommand().split("&");
			System.out.println("delete row by id : " + commands[0]);
			if (!commands[0].equals("null")){
				parent.deleteRow(Integer.parseInt(commands[0]));
			}
			deleteItemInList(Integer.parseInt(commands[1]));
			update();
			fireEditingStopped();
		}

		@Override
		public Component getTableCellEditorComponent(JTable table,
				Object value, boolean isSelected, int row, int column) {
			this.button.setActionCommand("" + value + "&" + row);
			return button;
		}
	}

	public void addNewRecord(E item) {
		jTableBinding.unbind();
		this.list.add(item);
		initDataBindings(this.list);
	}

	public List<E> getData() {
		return this.list;
	}
	
	protected void deleteItemInList(int row){
		list.remove(row);
	}

	public void update() {
		jTableBinding.unbind();
		initDataBindings(this.list);
	}
}
